﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;

namespace _011_1RemotingServerCallBack
{
    public interface ICallBack
    {
        [OneWay]
         void OnCallBack(string message);
    }
    
    public interface IServer
    {
        void Action(ICallBack callback);
    }
    public class Server : MarshalByRefObject,IServer 
    {
        public void Action(ICallBack callback)
        {
            Console.WriteLine("Server: start");
            callback.OnCallBack("Hello from server!");
            Console.WriteLine("Server: finish");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            //*
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 25701;
            TcpChannel chan = new TcpChannel(props, null, provider);
            //*/
            //TcpChannel chan = new TcpChannel(25701);


            ChannelServices.RegisterChannel(chan, true);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server),
                                                               "Server",
                                                               WellKnownObjectMode.SingleCall);
            Console.WriteLine("Server configured");
            Console.ReadLine();
        }
    }
}
